package com.yanqu.road.server.cmd.activity.apprentice;

import com.yanqu.road.entity.activity.apprentice.data.ApprenticeUserBanquetLog;
import com.yanqu.road.entity.activity.apprentice.data.ApprenticeUserData;
import com.yanqu.road.entity.activity.apprentice.data.ApprenticeUserPvpLog;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ApprenticeProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.apprentice.CrossApprenticeActivity;
import com.yanqu.road.server.manager.activity.apprentice.CrossApprenticeMgr;
import com.yanqu.road.server.manager.activity.apprentice.pb.ApprenticePb;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C2_APPRENTICE_GET_LOG_LIST, desc = "爱徒养成-日志列表")
public class CrossApprenticeGetLogListCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ApprenticeProto.ApprenticeGetLogListReqMsg reqMsg = ApprenticeProto.ApprenticeGetLogListReqMsg.parseFrom(packet.getBytes());
        ApprenticeProto.ApprenticeGetLogListRespMsg.Builder respMsg = ApprenticeProto.ApprenticeGetLogListRespMsg.newBuilder();
        CrossApprenticeActivity activity = CrossApprenticeMgr.getActivity(reqMsg.getActivityId());
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_APPRENTICE_GET_LOG_LIST, respMsg));
            return;
        }
        respMsg.setRet(0);
        ApprenticeUserData userData = activity.getUserData(packet.getPlayerId());
        if (userData != null) {
            if (reqMsg.getLogType() == ApprenticeProto.ApprenticeLogType.BANQUET.getNumber()) {
                for (ApprenticeUserBanquetLog log : userData.getBanquetLogs()) {
                    respMsg.addBanquetLogs(ApprenticePb.buildBanquetLog(log));
                }
                userData.setBanquetRedFlag(false);
            } else {
                for (ApprenticeUserPvpLog log : userData.getPvpLogs()) {
                    respMsg.addPvpLogs(ApprenticePb.buildPvpLog(log));
                }
                userData.setPvpRedFlag(false);
            }
            //同步一下userData
            activity.syncUserData(packet.getPlayerId());
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_APPRENTICE_GET_LOG_LIST, respMsg));
    }

    @Override
    public void sendData() {

    }
}
